layui.use(['layer', 'table', 'form'], function () {
    let table = layui.table,
        form = layui.form,
        layer = layui.layer;
    layui.layer.load(0, {shade: [0.5, '#fff']});
    /*查询所有公告的信息*/
    table.render({
        elem: '#grade-table'
        , height: 470
        , toolbar: true
        , url: "/getPaper"
        , where: {queryKey: '', userName: USERNAME, state: '定稿评分', type: 2}
        , cols: [
            [ //标题栏
                {field: 'bh', title: '编号', width: 90, align: 'center', sort: true},
                {field: 'title', title: '标题', align: 'center'},
                {field: 'sno', title: '学号', width: 140, align: 'center'},
                {field: 'sname', title: '姓名', width: 110, align: 'center'},
                {
                    field: 'title1', title: '所属学院', width: 130, align: 'center', templet: function (res) {
                        return res.title1.collegeName;
                    }
                },
                {
                    field: 'title1', title: '所属专业', width: 150, align: 'center', templet: function (res) {
                        return res.title1.majorName;
                    }
                },
                {
                    field: 'title1', title: '指导老师', width: 110, align: 'center', templet: function (res) {
                        return res.title1.tName;
                    }
                },
                {
                    field: 'title1', title: '老师工号', width: 130, align: 'center', templet: function (res) {
                        return res.title1.tno;
                    }
                },
                {
                    field: 'adviserGrade', title: '分数', width: 80, align: 'center', templet: function (res) {
                        if (res.adviserGrade === null) {
                            return '<span style="color: red"> -- </span>'
                        } else {
                            let grade = parseInt(res.adviserGrade.grade1) + parseInt(res.adviserGrade.grade2) + parseInt(res.adviserGrade.grade3) + parseInt(res.adviserGrade.grade4)
                                + parseInt(res.adviserGrade.grade5) + parseInt(res.adviserGrade.grade6)
                            if (grade < 60) {
                                return '<span style="color: red">' + grade + '</span>'
                            } else if (grade > 80) {
                                return '<span style="color: #009f95">' + grade + '</span>'
                            } else {
                                return '<span style="color: black">' + grade + '</span>'
                            }
                        }
                    }
                },
                {
                    field: 'state', title: '论文阶段', width: 130, align: 'center', templet: function (res) {
                        if (res.state === '定稿评分') {
                            return '<span style="color: #009688">定稿评分</span>'
                        } else {
                            return '<span style="color: #999999">' + res.state + '</span>'
                        }

                    }
                },

                {
                    field: 'adviserGrade', title: '评分状态', width: 140, align: 'center', templet: function (res) {
                        if (res.adviserGrade === null) {
                            return '<span style="color: red">待评分</span>'
                        } else {
                            if (res.adviserGrade.gradeState === null || res.adviserGrade.gradeState === '') {
                                return '<span style="color: red">待评分</span>'
                            } else if (res.adviserGrade.gradeState === '审核中') {
                                return '<span style="color: blue">审核中</span>'
                            } else if (res.adviserGrade.gradeState === '审核完成') {
                                return '<span style="color: #009688">审核完成</span>'
                            } else {
                                return '<span style="color: red">待评分</span>'
                            }
                        }
                    }
                },


                {title: '课题详情', align: 'center', width: 120, toolbar: '#task-table-tool'},
                {title: '评分操作', align: 'center', width: 180, toolbar: '#assignment-table-tool'},
                {field: 'title1', title: '课题', width: 120, align: 'center', hide: true},
                {field: 'adviserGrade', title: '任务书', width: 100, align: 'center', hide: true},
            ]
        ]

        // ,skin: 'line' //表格风格
        , even: true
        , page: true
        , limits: [7, 14, 21]
        , limit: 7
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });


    //监听表头事件  监听对象  allUSer
    table.on('toolbar(grade-table)', function (obj) {

        //查询事件
        if (obj.event === 'query') {  //按关键字搜索事件
            let queryKey = $('#queryKey').val().trim(); //获取搜索的关键字
            if (queryKey === null || queryKey === '') {
                layer.msg('请输入要查询内容', {offset: '250px', time: 1500, anim: 6, area: ['200px', '50px']});
                return false;
            }
            layer.alert(queryKey);
            table.reload('grade-table', {
                where: {queryKey: queryKey, userName: USERNAME, state: '定稿评分', type: 2}
            });


        } else if (obj.event === 'add') {  //查询全部
            table.reload('grade-table', {
                where: {queryKey: '', userName: USERNAME, state: '定稿评分', type: 2}
            });

        }
    });


    /*监听操作栏事件*/
    table.on('tool(grade-table)', function (obj) {
        let data = obj.data;  //json格式的数据
        if (obj.event === 'taskDetail') {  //编辑事件
            layer.open({
                type: 1
                , offset: 50
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'query-grade') {  //查看详情
            if (data.adviserGrade === null) {
                layer.msg('该论文还未定稿评分，无法进行查看操作', {anim: 6});
                return false;
            } else if (data.adviserGrade.gradeState === '审核完成' || data.adviserGrade.gradeState === '审核中') {
                layer.open({
                    type: 1
                    , offset: 0
                    , skin: 'layui-layer-rim'
                    , area: ['1040px', '800px']
                    , content: $("#teacher-grade--detail-box")
                    , title: '中期检查'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        setMsg(data);
                    }
                    , end: function () {
                        $("#teacher-grade--detail-box").css("display", 'none');
                    }
                });
            } else {
                layer.msg('此操作只有提交定稿评分才可以操作', {anim: 6});
                return false;
            }
        } else if (obj.event === 'edit-grade') {  //评分
            if (data.state !== '定稿评分') {
                layer.msg('该论文不处于定稿评分状态，无法进行评分操作', {anim: 6})
                return false;
            }
            if (data.adviserGrade === null) {
                layer.open({
                    type: 1
                    , offset: 0
                    , skin: 'layui-layer-rim'
                    , area: ['1040px', '800px']
                    , content: $("#teacher-grade-box")
                    , title: '定稿评分'
                    , success: function (layerObj) {
                        $(".layui-layer-shade").appendTo(layerObj.parent());
                        $("#sNO").val(data.sno);
                        $("#sName").val(data.sname);
                        $("#major").val(data.title1.majorName);
                        $("#title").val(data.title);
                    }
                    , end: function () {
                        $("#teacher-grade-box").css("display", 'none');
                    }
                });
            } else if (data.adviserGrade.gradeState === '审核中') {
                layer.msg('该论文定稿评分处于审核状态，你可点击<i class="layui-icon layui-icon-search"></i>查看详情', {anim: 6})
                return false;
            } else if (data.adviserGrade.gradeState === '审核完成') {
                layer.msg('该论文定稿评分已完成，你可点击<i class="layui-icon layui-icon-search"></i>查看详情', {anim: 6})
                return false;
            }
        } else if (obj.event === 'upload-grade-file') {  //导出word
            if (data.adviserGrade === null) {
                layer.msg('该论文还未定稿评分，无法进行查看操作', {anim: 6});
                return false;
            } else if (data.adviserGrade.gradeState === '审核完成' || data.adviserGrade.gradeState === '审核中') {
                setMsg(data);
                let fileName = data.sno + data.sname + '的定稿评分';
                $("#teacher-grade--detail-box").wordExport(fileName);
            } else {
                layer.msg('此操作只有提交定稿评分才可以操作', {anim: 6});
                return false;
            }


        }
    });

    layui.layer.closeAll('loading');


    /*提交成绩*/
    form.on('submit(add-grade)', function (data) {
        //console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
        //console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
        // console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
        //初步检测数据
        if (!checkGrade(data)) {
            return false;
        }
        let gradeData = JSON.stringify(data.field);
        let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
        console.log(gradeData)
        $.ajax({
            url: "/addGrade",
            type: 'post',
            data: {
                gradeData: gradeData, userName: USERNAME
            },
            dataType: 'json',
            success: function (data) {  //调用接口成功
                if (data === true || data === 'true') {
                    layer.close(loadPage);
                    layer.closeAll('page');
                    layui.layer.msg('提交定稿评分成功！', {offset: '300px', time: 2500, anim: 0});
                    layui.table.reload('grade-table', {
                        where: {queryKey: '', userName: USERNAME, state: '定稿评分', type: 2}
                    });
                } else {
                    layui.layer.close(loadPage);
                    layui.layer.alert('提交定稿评分失败！');
                }
            },
            error: function () {  //接口错误
                layui.layer.close(loadPage);
                layui.layer.alert('服务器出错啦,请稍后再试！');
            }
        });


        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

});

//设置详情的基本信息
function setMsg(data) {
    //接口查询
    //学生基本信息
    $("#sNO-detail").html(data.sno);
    $("#sName-detail").html(data.sname);
    $("#major-detail").html(data.title1.majorName);
    $("#title-detail").html(data.title);

    //论文基本信息
    $("#assignment").html(data.adviserGrade.assignment); //任务书
    $("#report").html(data.adviserGrade.report); //开题报告
    $("#textPart").html(data.adviserGrade.textPart); //正文
    $("#wordSum").html(data.adviserGrade.paperWord);//论文字数(个)
    $("#abstract").html(data.adviserGrade.abstract1);//中、英摘要
    $("#checkAndAccept").html(data.adviserGrade.checkAccept);//软硬件验收
    $("#isTruant").html(data.adviserGrade.isTruant);//有无旷课
    $("#truantDays").html(data.adviserGrade.truantDays);//旷课天数
    $("#isReply").html(data.adviserGrade.isReply);//是否同意参加答辩
    $("#remark").html(data.adviserGrade.remark);//指导教师评语

    //各项分数
    let grade1 = parseInt(data.adviserGrade.grade1);
    let grade2 = parseInt(data.adviserGrade.grade2);
    let grade3 = parseInt(data.adviserGrade.grade3);
    let grade4 = parseInt(data.adviserGrade.grade4);
    let grade5 = parseInt(data.adviserGrade.grade5);
    let grade6 = parseInt(data.adviserGrade.grade6);
    let grade = grade1 + grade2 + grade3 + grade4 + grade5 + grade6;
    $("#detail-grade").html(grade);//总分
    $("#grade1").html(grade1);//详情分数1
    $("#grade2").html(grade2);//详情分数2
    $("#grade3").html(grade3);//详情分数3
    $("#grade4").html(grade4);//详情分数4
    $("#grade5").html(grade5);//详情分数5
    $("#grade6").html(grade6);//详情分数6
}

function checkGrade(data) {
    if ((data.field.isTruant === '无' && parseInt(data.field.truantDays) !== 0) || parseInt(data.field.truantDays) < 0) {
        layer.msg('旷课天数，不符合要求', {anim: 6});
        $("input[name = 'truantDays']").focus();
        return false;
    }

    // 论文字符
    if (parseInt(data.field.wordSum) < 0) {
        layer.msg('论文字数不小于0', {anim: 6});
        $("input[name = 'wordSum']").focus();
        return false;
    }

    if (parseInt(data.field.grade1) < 0 || parseInt(data.field.grade1) > 15) {
        layer.msg('第 1 项评分不符合要求', {anim: 6});
        $("input[name = 'grade1']").focus();
        return false;
    }

    if (parseInt(data.field.grade2) < 0 || parseInt(data.field.grade2) > 20) {
        layer.msg('第 2 项评分不符合要求', {anim: 6});
        $("input[name = 'grade2']").focus();
        return false;
    }

    if (parseInt(data.field.grade3) < 0 || parseInt(data.field.grade3) > 10) {
        layer.msg('第 3 项评分不符合要求', {anim: 6});
        $("input[name = 'grade3']").focus();
        return false;
    }

    if (parseInt(data.field.grade4) < 0 || parseInt(data.field.grade4) > 25) {
        layer.msg('第 4 项评分不符合要求', {anim: 6});
        $("input[name = 'grade4']").focus();
        return false;
    }

    if (parseInt(data.field.grade5) < 0 || parseInt(data.field.grade5) > 15) {
        layer.msg('第 5 项评分不符合要求', {anim: 6});
        $("input[name = 'grade5']").focus();
        return false;
    }

    if (parseInt(data.field.grade6) < 0 || parseInt(data.field.grade6) > 15) {
        layer.msg('第 6 项评分不符合要求', {anim: 6});
        $("input[name = 'grade6']").focus();
        return false;
    }

    return true;
}






